﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data;
using System.Collections;
using System.Reflection;
using System.Data.SqlClient;
using System.Configuration;
using Projectmanagement.Entity;

namespace Projectmanagement.Utl
{
    public class MappingRecords
    {
        public Type DbContextType { get; set; }
        public string primaryKeyName { get; set; }
    }
    public class CH
    {
        public static DB DB
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["DB"] == null)
                {
                    HttpContext.Current.Items["DB"] = new DB();
                }
             
                return HttpContext.Current.Items["DB"] as DB;
            }
            set
            {
                if (HttpContext.Current != null)
                    HttpContext.Current.Items["DB"] = value;
            }
        }

        public static DBAccount DBAccount
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["DBAccount"] == null)
                {
                    HttpContext.Current.Items["DBAccount"] = new DBAccount();
                }

                return HttpContext.Current.Items["DBAccount"] as DBAccount;
            }
            set
            {
                if (HttpContext.Current != null)
                    HttpContext.Current.Items["DBAccount"] = value;
            }
        }

        static DbSet<T> GetAllRowData<T>()
           where T : class
        {
            var re = DB.Set<T>();
            return re;
        }

        public static IQueryable<T> GetAllDatas<T>() where T : EntityBase
        {
            return DB.Set<T>().AsQueryable();
        }

  

    

        public static T GetDataByID<T>(int? id) where T : EntityBase
        {

            return DB.Set<T>().Find(id);
        }

     

        static void AddCreatedStamp(object entity)
        {
            if (entity is EntityBase)
            {
                var eb = entity as EntityBase;
                eb.CreatedDate = DateTime.Now;
                eb.Creator = Employee.CurrentUserName;
            }
        }

        static void AddModifiedStamp(object entity)
        {
            if (entity is EntityBase)
            {
                var eb = entity as EntityBase;
                eb.ModifiedDate = DateTime.Now;
                eb.ModifiedUser = Employee.CurrentUserName;
            }
        }

        public static int Create<T>(T entity)
            where T : class
        {
            AddCreatedStamp(entity);
            DB.Set<T>().Add(entity);
            var re = DB.SaveChanges();
            return re;
        }

    

        public static int Edit<T>(T entity)
             where T : class
        {
            AddModifiedStamp(entity);
            var c = DB.Entry(entity);
            c.State = System.Data.EntityState.Modified;
            var re = DB.SaveChanges();
            return re;
        }


        public static int Delete<T>(int? id)
        where T : class
        {
            T record = DB.Set<T>().Find(id);
            DB.Set<T>().Remove(record);
            return DB.SaveChanges();
        }
    }
}